/**********************************************************************
 * Project : WoWGuildRoster (http://www.codeplex.com/WowGuildRoster/)
 * Module  : DotNetWarcraft.WoWGuildRoster.UI.DNN.WGR_Settings
 **********************************************************************
 * 
 * 
 * 
 **********************************************************************
 * License : All code is subject to licensing terms per the Microsoft
 *   Permissive License (Ms-PL) v1.1. For full license information,
 *   refer to the packaged license.txt file.
 **********************************************************************
 * Revision History:
 * -----------------
 * 2007-08-26 -- Cliff Simpkins: Initial Version
 **********************************************************************
 */
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Exceptions;

using WGR_Business = DotNetWarcraft.WoWGuildRoster.Business;

namespace DotNetWarcraft.WoWGuildRoster.UI.DNN {
  public partial class WGR_Settings : DotNetNuke.Entities.Modules.ModuleSettingsBase {
    protected const String _className = "DotNetWarcraft.WoWGuildRoster.UI.DNN.WGR_Settings";
    #region "Public Properties"
    protected bool TestDisplay {
      get {
        return cbTestDisplay.Checked;
      }
      set {
        cbTestDisplay.Checked = value;
      }
    }
    protected String EntriesPerPage {
      get {
        return txtEntriesPerPage.Text;
      }
      set {
        txtEntriesPerPage.Text = value;
      }
    }
    protected String ListsPerRow {
      get {
        return txtListsPerRow.Text;
        }
      set {
        txtListsPerRow.Text = value;
        }
      }

    protected String DisplayType {
      get {
        if (drpDisplayType.Items.Count < 1) {
          this.PopulateDropDown_DisplayTypes();
        }
        return drpDisplayType.SelectedValue;
      }
      set {
        if (drpDisplayType.Items.Count < 1) {
          this.PopulateDropDown_DisplayTypes();
        }

        drpDisplayType.ClearSelection();
        try {
          drpDisplayType.Items.FindByValue(value).Selected = true;
          } catch {
            //In case we encounter an exception (the previous option is no longer available),
            //  we default to displaying all classes.
          drpDisplayType.Items.FindByValue(WGR_Business.WGRDnnConstants.ROSTER_DISPLAYTYPE_ALL_CLASSES).Selected = true;
          }
      }
    }

    protected String SelectedClass {
      get {
        if (drpClassSelection.Items.Count < 1) {
          this.PopulateDropDown_Classes();
        }
        return drpClassSelection.SelectedValue;
      }
      set {
        if (drpClassSelection.Items.Count < 1) {
          this.PopulateDropDown_Classes();
        }

        drpClassSelection.ClearSelection();
        try {
          drpClassSelection.Items.FindByValue(value).Selected = true;
        } catch (Exception _e) {
          (new Business.Exceptions.WGRGeneralError(_e, _className + "SelectedClass(" + value.ToString() + ")")).WriteExceptionToDB();
          drpClassSelection.Items.FindByValue("").Selected = true;
        }
      }
    }
    protected String SelectedProfession {
      get {
        if (drpProfSelection.Items.Count < 1) {
          this.PopulateDropDown_Professions();
        }
        return drpProfSelection.SelectedValue;
      }
      set {
        if (drpProfSelection.Items.Count < 1) {
          this.PopulateDropDown_Professions();
        }

        drpProfSelection.ClearSelection();
        try {
          drpProfSelection.Items.FindByValue(value).Selected = true;
        } catch (Exception _e) {
          (new Business.Exceptions.WGRGeneralError(_e, _className + "SelectedProfession(" + value.ToString() + ")")).WriteExceptionToDB();
          drpProfSelection.Items.FindByValue("").Selected = true;
        }
      }
    }
    protected String SelectedReputation {
      get {
        if (drpRepSelection.Items.Count < 1) {
          this.PopulateDropDown_Reputations();
        }
        return drpRepSelection.SelectedValue;
      }
      set {
        if (drpRepSelection.Items.Count < 1) {
          this.PopulateDropDown_Reputations();
        }

        drpRepSelection.ClearSelection();
        try {
          drpRepSelection.Items.FindByValue(value).Selected = true;
        } catch (Exception _e) {
          (new Business.Exceptions.WGRGeneralError(_e, _className + "SelectedReputation(" + value.ToString() + ")")).WriteExceptionToDB();
          drpRepSelection.Items.FindByValue("").Selected = true;
        }
      }
    }
    protected String SelectedGuildRank {
      get {
        if (drpGuildRankSelection.Items.Count < 1) {
          this.PopulateDropDown_GuildRanks();
        }
        return drpGuildRankSelection.SelectedValue;
      }
      set {
        if (drpGuildRankSelection.Items.Count < 1) {
          this.PopulateDropDown_GuildRanks();
        }

        drpGuildRankSelection.ClearSelection();
        try {
          drpGuildRankSelection.Items.FindByValue(value).Selected = true;
        } catch (Exception _e) {
          (new Business.Exceptions.WGRGeneralError(_e, _className + "SelectedGuildRank(" + value.ToString() + ")")).WriteExceptionToDB();
          drpGuildRankSelection.Items.FindByValue("").Selected = true;
        }
      }
    }

    protected String MinLevelRange {
      get {
        return txtMinLevel.Text;
      }
      set {
        txtMinLevel.Text = value;
      }
    }
    protected String MaxLevelRange {
      get {
        return txtMaxLevel.Text;
      }
      set {
        txtMaxLevel.Text = value;
      }
    }

    protected String MinGRankRange {
      get {
        return txtMinGRank.Text;
        }
      set {
        txtMinGRank.Text = value;
        }
      }
    protected String MaxGRankRange {
      get {
        return txtMaxGRank.Text;
        }
      set {
        txtMaxGRank.Text = value;
        }
      }

    protected String MemberRoles {
      get {
        if (cblMemberRoles == null) {
          return "";
        }

        String _memberRoles = "";
        foreach (ListItem _li in cblMemberRoles.Items) {
          if (_li.Selected) {
            _memberRoles += _li.Value.ToString() + ";";
          }
        }
        return _memberRoles;
      }
      set {
        if (cblMemberRoles.Items.Count < 1) {
          this.BindRoles();
        }

        String[] _memberRoles = value.Split(';');

        foreach (String _memberRole in _memberRoles) {
          if (_memberRole.Trim() != "") {
            ListItem _li = cblMemberRoles.Items.FindByValue(_memberRole);
            if (_li != null) {
              _li.Selected = true;
            }
          }
        }
      }
    }
    protected String OfficerRoles {
      get {
        if (cblOfficerRoles == null) {
          return "";
        }

        String _officerRoles = "";
        foreach (ListItem _li in cblOfficerRoles.Items) {
          if (_li.Selected) {
            _officerRoles += _li.Value.ToString() + ";";
          }
        }
        return _officerRoles;
      }
      set {
        if (cblOfficerRoles.Items.Count < 1) {
          this.BindRoles();
        }

        String[] _approvalRoles = value.Split(';');

        foreach (String _approvalRole in _approvalRoles) {
          if (_approvalRole.Trim() != "") {
            ListItem _li = cblOfficerRoles.Items.FindByValue(_approvalRole);
            if (_li != null) {
              _li.Selected = true;
            }
          }
        }
      }
    }

    protected bool ShowMainsOnly {
      get {
        return chbShowMainsOnly.Checked;
      }
      set {
        chbShowMainsOnly.Checked = value;
      }
    }

    protected bool ArmoryStyleCols {
      get {
        return chbArmoryStyleCols.Checked;
      }
      set {
        chbArmoryStyleCols.Checked = value;
      }
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e) {
      if (DotNetNuke.Framework.AJAX.IsInstalled()) {
        DotNetNuke.Framework.AJAX.RegisterScriptManager();
      }

    }

    #region "Protected Events - Control Visibility"
    #endregion
    #region "Protected Events - Data Interaction"
    public override void LoadSettings() {
      try {
        if (!Page.IsPostBack) {
          //This is a hack that probably needs to be better designed
          //We need to make a call to the DnnGuildRosterController to make sure the connection strings
          // are set properly
          String _ver = WGR_Business.DnnGuildRosterController.VersionNumber;

          this.BindRoles();

          if (Settings.Contains(WGR_Business.WGRDnnConstants.SETTING_OFFICER_ROLES)) {
            this.OfficerRoles = ((string)Settings[WGR_Business.WGRDnnConstants.SETTING_OFFICER_ROLES]);
          } else {
            this.OfficerRoles = PortalSettings.AdministratorRoleId.ToString();
          }

          if (Settings.Contains(WGR_Business.WGRDnnConstants.SETTING_MEMBER_ROLES)) {
            this.MemberRoles = ((string)Settings[WGR_Business.WGRDnnConstants.SETTING_MEMBER_ROLES]);
          } else {
            this.MemberRoles = PortalSettings.AdministratorRoleId.ToString();
          }

          if (Settings.Contains(WGR_Business.WGRDnnConstants.SETTING_DISPLAY_TESTING_MODE)) {
            this.TestDisplay = (Convert.ToBoolean(Settings[WGR_Business.WGRDnnConstants.SETTING_DISPLAY_TESTING_MODE].ToString()));
          } else {
            this.TestDisplay = false;
          }

          if (Settings.Contains(WGR_Business.WGRDnnConstants.SETTING_LINES_PER_PAGE)) {
            this.EntriesPerPage = Settings[WGR_Business.WGRDnnConstants.SETTING_LINES_PER_PAGE].ToString();
          } else {
            this.EntriesPerPage = WGR_Business.WGRDnnConstants.SETTING_DEFAULT_LINES_PER_PAGE.ToString();
          }

          if (Settings.Contains(WGR_Business.WGRDnnConstants.SETTING_LISTS_PER_ROW)) {
            this.ListsPerRow = Settings[WGR_Business.WGRDnnConstants.SETTING_LISTS_PER_ROW].ToString();
            } else {
            this.ListsPerRow = WGR_Business.WGRDnnConstants.SETTING_DEFAULT_LISTS_PER_ROW.ToString();
            }

          if (Settings.Contains(WGR_Business.WGRDnnConstants.SETTING_DISPLAY_TYPE)) {
            this.DisplayType = Settings[WGR_Business.WGRDnnConstants.SETTING_DISPLAY_TYPE].ToString();
          } else {
            this.DisplayType = WGR_Business.WGRDnnConstants.ROSTER_DISPLAYTYPE_ALL_CLASSES;
          }

          if (Settings.Contains(WGR_Business.WGRDnnConstants.SETTING_DISPLAY_CLASS)) {
            this.SelectedClass = Settings[WGR_Business.WGRDnnConstants.SETTING_DISPLAY_CLASS].ToString();
          } else {
            this.SelectedClass = "";
          }

          if (Settings.Contains(WGR_Business.WGRDnnConstants.SETTING_DISPLAY_PROF)) {
            this.SelectedProfession = Settings[WGR_Business.WGRDnnConstants.SETTING_DISPLAY_PROF].ToString();
          } else {
            this.SelectedProfession = "";
          }

          if (Settings.Contains(WGR_Business.WGRDnnConstants.SETTING_DISPLAY_REPUTATION)) {
            this.SelectedReputation = Settings[WGR_Business.WGRDnnConstants.SETTING_DISPLAY_REPUTATION].ToString();
          } else {
            this.SelectedReputation = "";
          }

          if (Settings.Contains(WGR_Business.WGRDnnConstants.SETTING_DISPLAY_RANK)) {
            this.SelectedGuildRank = Settings[WGR_Business.WGRDnnConstants.SETTING_DISPLAY_RANK].ToString();
          } else {
            this.SelectedGuildRank = "";
          }

          if (Settings.Contains(WGR_Business.WGRDnnConstants.SETTING_DISPLAY_MAXLEVEL)) {
            this.MaxLevelRange = Settings[WGR_Business.WGRDnnConstants.SETTING_DISPLAY_MAXLEVEL].ToString();
          } else {
            this.MaxLevelRange = WGR_Business.WGRDnnConstants.SETTING_DEFAULT_MAXLEVEL.ToString();
          }

          if (Settings.Contains(WGR_Business.WGRDnnConstants.SETTING_DISPLAY_MINLEVEL)) {
            this.MinLevelRange = Settings[WGR_Business.WGRDnnConstants.SETTING_DISPLAY_MINLEVEL].ToString();
          } else {
            this.MinLevelRange = WGR_Business.WGRDnnConstants.SETTING_DEFAULT_MINLEVEL.ToString();
          }

          if (Settings.Contains(WGR_Business.WGRDnnConstants.SETTING_DISPLAY_MAXGRANK)) {
            this.MaxGRankRange = Settings[WGR_Business.WGRDnnConstants.SETTING_DISPLAY_MAXGRANK].ToString();
            } else {
            this.MaxGRankRange = WGR_Business.WGRDnnConstants.SETTING_DEFAULT_MAXGRANK.ToString();
            }

          if (Settings.Contains(WGR_Business.WGRDnnConstants.SETTING_DISPLAY_MINGRANK)) {
            this.MinGRankRange = Settings[WGR_Business.WGRDnnConstants.SETTING_DISPLAY_MINGRANK].ToString();
            } else {
            this.MinGRankRange = WGR_Business.WGRDnnConstants.SETTING_DEFAULT_MINGRANK.ToString();
            }

          

          if (Settings.Contains(WGR_Business.WGRDnnConstants.SETTING_MAINS_ONLY)) {
            int _outValue = 0;

            if (int.TryParse(Settings[WGR_Business.WGRDnnConstants.SETTING_MAINS_ONLY].ToString(), out _outValue)) {
              this.ShowMainsOnly = ((_outValue == 1) ? true : false);
            } else { this.ShowMainsOnly = false; }

          } else {
            this.ShowMainsOnly = false;
          }

          if (Settings.Contains(WGR_Business.WGRDnnConstants.SETTING_ARMORY_STYLE_COLS)) {
            int _outValue = 0;

            if (int.TryParse(Settings[WGR_Business.WGRDnnConstants.SETTING_ARMORY_STYLE_COLS].ToString(), out _outValue)) {
              this.ArmoryStyleCols = ((_outValue == 1) ? true : false);
              } else { this.ArmoryStyleCols = false; }

            } else {
            this.ArmoryStyleCols = false;
            }

          List<WGR_Business.GuildRoster> _allRosters = WGR_Business.DnnGuildRosterController.GetRosters();
          List<WGR_Business.GuildRosterModule> _currRosterMods = WGR_Business.DnnGuildRosterController.GetRostersByModule(this.ModuleId, this.PortalId);

          List<WGR_Business.GuildRosterModule> _dispRosterMods = new List<WGR_Business.GuildRosterModule>();

          foreach (WGR_Business.GuildRoster _roster in _allRosters) {
            bool _added = false;
            foreach (WGR_Business.GuildRosterModule _rosterMod in _currRosterMods) {
              if (_roster.Roster_ID == _rosterMod.Roster_ID) {
                _rosterMod.Show_Guild_Bool = true;
                _rosterMod.Guild_Name = _roster.Guild_Name;
                _dispRosterMods.Add(_rosterMod);
                _added = true;
              }
            }

            if (!_added) {
              WGR_Business.GuildRosterModule _newRosterMod = new WGR_Business.GuildRosterModule();
              _newRosterMod.Module_ID = this.ModuleId;
              _newRosterMod.Portal_ID = this.PortalId;
              _newRosterMod.Roster_ID = _roster.Roster_ID;
              _newRosterMod.Show_Guild_Bool = false;
              _newRosterMod.Guild_Name = _roster.Guild_Name;

              _dispRosterMods.Add(_newRosterMod);
            }
          }
          dgRosterList.DataSource = _dispRosterMods;
          dgRosterList.DataBind();
        }

        ToggleDisplayTypeVisibility();

      } catch (Exception exc) {
        Exceptions.ProcessModuleLoadException(this, exc);
      }
    }
    public override void UpdateSettings() {
      try {
        DotNetNuke.Entities.Modules.ModuleController objModules = new DotNetNuke.Entities.Modules.ModuleController();
        objModules.UpdateTabModuleSetting(TabModuleId, WGR_Business.WGRDnnConstants.SETTING_OFFICER_ROLES, this.OfficerRoles);
        objModules.UpdateTabModuleSetting(TabModuleId, WGR_Business.WGRDnnConstants.SETTING_MEMBER_ROLES, this.MemberRoles);
        objModules.UpdateTabModuleSetting(TabModuleId, WGR_Business.WGRDnnConstants.SETTING_DISPLAY_TESTING_MODE, this.TestDisplay.ToString());
        objModules.UpdateTabModuleSetting(TabModuleId, WGR_Business.WGRDnnConstants.SETTING_LINES_PER_PAGE, this.EntriesPerPage.ToString());
        objModules.UpdateTabModuleSetting(TabModuleId, WGR_Business.WGRDnnConstants.SETTING_LISTS_PER_ROW, this.ListsPerRow.ToString());
        objModules.UpdateTabModuleSetting(TabModuleId, WGR_Business.WGRDnnConstants.SETTING_DISPLAY_TYPE, this.DisplayType.ToString());
        objModules.UpdateTabModuleSetting(TabModuleId, WGR_Business.WGRDnnConstants.SETTING_DISPLAY_CLASS, this.SelectedClass.ToString());
        objModules.UpdateTabModuleSetting(TabModuleId, WGR_Business.WGRDnnConstants.SETTING_DISPLAY_PROF, this.SelectedProfession.ToString());
        objModules.UpdateTabModuleSetting(TabModuleId, WGR_Business.WGRDnnConstants.SETTING_DISPLAY_REPUTATION, this.SelectedReputation.ToString());
        objModules.UpdateTabModuleSetting(TabModuleId, WGR_Business.WGRDnnConstants.SETTING_DISPLAY_RANK, this.SelectedGuildRank.ToString());
        objModules.UpdateTabModuleSetting(TabModuleId, WGR_Business.WGRDnnConstants.SETTING_MAINS_ONLY, (this.ShowMainsOnly ? "1" : "0"));
        objModules.UpdateTabModuleSetting(TabModuleId, WGR_Business.WGRDnnConstants.SETTING_ARMORY_STYLE_COLS, (this.ArmoryStyleCols ? "1" : "0"));

        objModules.UpdateTabModuleSetting(TabModuleId, WGR_Business.WGRDnnConstants.SETTING_DISPLAY_MAXLEVEL, this.MaxLevelRange.ToString());
        objModules.UpdateTabModuleSetting(TabModuleId, WGR_Business.WGRDnnConstants.SETTING_DISPLAY_MINLEVEL, this.MinLevelRange.ToString());
        objModules.UpdateTabModuleSetting(TabModuleId, WGR_Business.WGRDnnConstants.SETTING_DISPLAY_MAXGRANK, this.MaxGRankRange.ToString());
        objModules.UpdateTabModuleSetting(TabModuleId, WGR_Business.WGRDnnConstants.SETTING_DISPLAY_MINGRANK, this.MinGRankRange.ToString());

        List<WGR_Business.GuildRosterModule> _rosters = WGR_Business.DnnGuildRosterController.GetRostersByModule(this.ModuleId, this.PortalId);

        foreach (DataGridItem _dgi in dgRosterList.Items) {
          if (_dgi.ItemType == ListItemType.AlternatingItem || _dgi.ItemType == ListItemType.Item) {
            try {
              int _rosterID = int.Parse(_dgi.Cells[0].Text);
              int _moduleID = int.Parse(_dgi.Cells[1].Text);
              int _portalID = int.Parse(_dgi.Cells[2].Text);
              String _guildName = _dgi.Cells[3].Text;

              bool _show = ((System.Web.UI.WebControls.CheckBox)_dgi.FindControl("cbShowGuild")).Checked;
              bool _lua = ((System.Web.UI.WebControls.CheckBox)_dgi.FindControl("cbAllowLua")).Checked;

              if (_show) {
                WGR_Business.GuildRosterModule _rosterMod = new WGR_Business.GuildRosterModule(this.ModuleId, this.PortalId, _rosterID, _guildName, _lua);
                WGR_Business.DnnGuildRosterController.AddRosterModuleEntry(_rosterMod);
              } else {
                WGR_Business.DnnGuildRosterController.DeleteRosterModuleEntry(this.ModuleId, this.PortalId, _rosterID);
              }

            } catch {
            }
          }
        }

        WGR_Business.DnnGuildRosterController.ForceLocalCacheRefresh();
        Response.Redirect(Globals.NavigateURL(), true);
      } catch (Exception exc) {
        Exceptions.ProcessModuleLoadException(this, exc);
      }
    }

    protected void dgRosterList_ItemCreated(object sender, DataGridItemEventArgs e) {
      try {
        if (e.Item.DataItem != null) {
          switch (e.Item.ItemType) {
            case ListItemType.Item:
            case ListItemType.AlternatingItem:
              ((System.Web.UI.WebControls.CheckBox)e.Item.FindControl("cbShowGuild")).Checked = ((WGR_Business.GuildRosterModule)e.Item.DataItem).Show_Guild_Bool;
              ((System.Web.UI.WebControls.CheckBox)e.Item.FindControl("cbAllowLua")).Checked = ((WGR_Business.GuildRosterModule)e.Item.DataItem).Can_Load_LUA_Bool;
              break;
            default:
              break;
          }
        }
      } catch {
      }
    }

    protected void BindRoles() {
      DotNetNuke.Security.Roles.RoleController _roleCtrl = new DotNetNuke.Security.Roles.RoleController();

      System.Collections.ArrayList _roles;

      _roles = _roleCtrl.GetPortalRoles(this.PortalId);

      cblOfficerRoles.Items.Clear();
      cblMemberRoles.Items.Clear();

      if (_roles != null) {
        foreach (DotNetNuke.Security.Roles.RoleInfo _role in _roles) {
          ListItem _liOfficer = new ListItem(_role.RoleName);
          _liOfficer.Value = _role.RoleID.ToString();
          cblOfficerRoles.Items.Add(_liOfficer);

          ListItem _liMember = new ListItem(_role.RoleName);
          _liMember.Value = _role.RoleID.ToString();
          cblMemberRoles.Items.Add(_liMember);
        }
      }

    }

    protected void PopulateDropDown_DisplayTypes() {
      try {
        ListItem _liGuildNotes = new ListItem("Guild Notes", WGR_Business.WGRDnnConstants.ROSTER_DISPLAYTYPE_NOTES);
        ListItem _liGuildClasses = new ListItem("Guild Classes (All Classes)", WGR_Business.WGRDnnConstants.ROSTER_DISPLAYTYPE_ALL_CLASSES);
        ListItem _liGuildProfessions = new ListItem("Guild Professions (All Profs)", WGR_Business.WGRDnnConstants.ROSTER_DISPLAYTYPE_ALL_PROFS);
        ListItem _liGuildClass = new ListItem("Class Listing (Single Class)", WGR_Business.WGRDnnConstants.ROSTER_DISPLAYTYPE_ONE_CLASS);
        ListItem _liGuildProfession = new ListItem("Profession Listing (Single Prof)", WGR_Business.WGRDnnConstants.ROSTER_DISPLAYTYPE_ONE_PROF);
        ListItem _liGuildReputation = new ListItem("Reputation Listing (Single Rep)", WGR_Business.WGRDnnConstants.ROSTER_DISPLAYTYPE_ONE_REP);
        ListItem _liGuildRanks = new ListItem("Guild Rank Listing (All Ranks)", WGR_Business.WGRDnnConstants.ROSTER_DISPLAYTYPE_ALL_RANKS);
        ListItem _liGuildRank = new ListItem("Guild Rank Listing (Single Rank)", WGR_Business.WGRDnnConstants.ROSTER_DISPLAYTYPE_ONE_RANK);
        ListItem _liGuildLevelsAll = new ListItem("Level Listing (All Levels)", WGR_Business.WGRDnnConstants.ROSTER_DISPLAYTYPE_BY_LEVEL_ALL);
        ListItem _liGuildLevelsRange = new ListItem("Level Listing (Level Range)", WGR_Business.WGRDnnConstants.ROSTER_DISPLAYTYPE_BY_LEVEL_RANGE);
        ListItem _liGuildGRanksAll = new ListItem("Guild Ranks (All Ranks)", WGR_Business.WGRDnnConstants.ROSTER_DISPLAYTYPE_BY_RANKS_ALL);
        ListItem _liGuildGRanksRange = new ListItem("Guild Ranks (Rank Range)", WGR_Business.WGRDnnConstants.ROSTER_DISPLAYTYPE_BY_RANKS_RANGE);

        drpDisplayType.Items.Add(_liGuildRanks);
        drpDisplayType.Items.Add(_liGuildClasses);
        drpDisplayType.Items.Add(_liGuildProfessions);
        drpDisplayType.Items.Add(_liGuildLevelsAll);
        drpDisplayType.Items.Add(_liGuildLevelsRange);
        drpDisplayType.Items.Add(_liGuildGRanksAll);
        drpDisplayType.Items.Add(_liGuildGRanksRange);
        //drpDisplayType.Items.Add(_liGuildNotes);
        drpDisplayType.Items.Add(_liGuildClass);
        drpDisplayType.Items.Add(_liGuildRank);
        drpDisplayType.Items.Add(_liGuildProfession);
        drpDisplayType.Items.Add(_liGuildReputation);

        drpDisplayType.ClearSelection();
        drpDisplayType.Items.FindByValue(WGR_Business.WGRDnnConstants.ROSTER_DISPLAYTYPE_ALL_CLASSES).Selected = true;

      } catch {
      }
    }
    protected void PopulateDropDown_Classes() {
      try {
        drpClassSelection.Items.Clear();
        ListItem _licBlank = new ListItem("None Selected", "");
        drpClassSelection.Items.Insert(0, _licBlank);

        List<String> _classList = WGR_Business.DnnGuildRosterController.GetDropDown_Classes();

        foreach (String _class in _classList) {
          ListItem _li = new ListItem(_class);
          drpClassSelection.Items.Add(_li);
        }

      } catch (Exception _e) {
        (new Business.Exceptions.WGRGeneralError(_e, _className + "PopulateDropDown_Classes()")).WriteExceptionToDB();
      }
    }
    protected void PopulateDropDown_Professions() {
      try {
        drpProfSelection.Items.Clear();
        ListItem _lipBlank = new ListItem("None Selected", "");
        drpProfSelection.Items.Insert(0, _lipBlank);

        List<String> _profList = WGR_Business.DnnGuildRosterController.GetDropDown_Professions();

        foreach (String _prof in _profList) {
          ListItem _li = new ListItem(_prof);
          drpProfSelection.Items.Add(_li);
        }

      } catch (Exception _e) {
        (new Business.Exceptions.WGRGeneralError(_e, _className + "PopulateDropDown_Professions()")).WriteExceptionToDB();
      }
    }
    protected void PopulateDropDown_Reputations() {
      try {
        drpRepSelection.Items.Clear();
        ListItem _lirBlank = new ListItem("None Selected", "");
        drpRepSelection.Items.Insert(0, _lirBlank);

        List<String> _repList = WGR_Business.DnnGuildRosterController.GetDropDown_Reputations();

        foreach (String _rep in _repList) {
          ListItem _li = new ListItem(_rep);
          //ListItem _li = new ListItem(_rep.Stat_Entry + " (" + _rep.Stat_Group + ")", _rep.Stat_Entry);
          drpRepSelection.Items.Add(_li);
        }
      } catch (Exception _e) {
        (new Business.Exceptions.WGRGeneralError(_e, _className + "PopulateDropDown_Reputations()")).WriteExceptionToDB();
      }
    }
    protected void PopulateDropDown_GuildRanks() {
      try {
        drpGuildRankSelection.Items.Clear();
        ListItem _lirBlank = new ListItem("None Selected", "");
        drpGuildRankSelection.Items.Insert(0, _lirBlank);

        //List<String> _rankList = WGR_Business.DnnGuildRosterController.GetDropDown_Reputations();

        for (int rank = 0; rank <= 10; rank++) {
          ListItem _li = new ListItem("Rank " + rank.ToString(), rank.ToString());
          drpGuildRankSelection.Items.Add(_li);
        }
      } catch (Exception _e) {
        (new Business.Exceptions.WGRGeneralError(_e, _className + "PopulateDropDown_GuildRanks()")).WriteExceptionToDB();
      }
    }
    #endregion

    protected void ToggleDisplayTypeVisibility() {

      drpClassSelection.Visible = false;
      drpProfSelection.Visible = false;
      drpRepSelection.Visible = false;
      drpGuildRankSelection.Visible = false;
      pnlLevelRange.Visible = false;
      pnlGRankRange.Visible = false;
      this.EnableLevelRangeValidators(false);
      this.EnableGRankRangeValidators(false);

      switch (this.DisplayType) {
        case WGR_Business.WGRDnnConstants.ROSTER_DISPLAYTYPE_ONE_CLASS:
          drpClassSelection.Visible = true;
          break;
        case WGR_Business.WGRDnnConstants.ROSTER_DISPLAYTYPE_ONE_PROF:
          drpProfSelection.Visible = true;
          break;
        case WGR_Business.WGRDnnConstants.ROSTER_DISPLAYTYPE_ONE_REP:
          drpRepSelection.Visible = true;
          break;
        case WGR_Business.WGRDnnConstants.ROSTER_DISPLAYTYPE_ONE_RANK:
          drpGuildRankSelection.Visible = true;
          break;
        case WGR_Business.WGRDnnConstants.ROSTER_DISPLAYTYPE_BY_RANKS_RANGE:
          pnlGRankRange.Visible = true;
          this.EnableGRankRangeValidators(true);
          break;
        case WGR_Business.WGRDnnConstants.ROSTER_DISPLAYTYPE_BY_LEVEL_RANGE:
          pnlLevelRange.Visible = true;
          this.EnableLevelRangeValidators(true);
          break;
      }

    }

    protected void EnableLevelRangeValidators(bool enabledFlag) {
      vldMaxLevel_int.EnableClientScript = enabledFlag;
      vldMaxLevel_max.EnableClientScript = enabledFlag;
      vldMaxLevel_min.EnableClientScript = enabledFlag;

      vldMinLevel_int.EnableClientScript = enabledFlag;
      vldMinLevel_max.EnableClientScript = enabledFlag;
      vldMinLevel_min.EnableClientScript = enabledFlag;
    }

    protected void EnableGRankRangeValidators(bool enabledFlag) {
      vldMaxGRank_int.EnableClientScript = enabledFlag;
      vldMaxGRank_max.EnableClientScript = enabledFlag;
      vldMaxGRank_min.EnableClientScript = enabledFlag;

      vldMinGRank_int.EnableClientScript = enabledFlag;
      vldMinGRank_max.EnableClientScript = enabledFlag;
      vldMinGRank_min.EnableClientScript = enabledFlag;
      }

    protected void drpDisplayType_SelectedIndexChanged(object sender, EventArgs e) {
      ToggleDisplayTypeVisibility();
    }

  }
}